package org.sea.mybator.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.velocity.VelocityContext;
import org.sea.mybator.model.JavaDao;
import org.sea.mybator.model.TableDesc;
import org.sea.mybator.util.Constants;
import org.sea.mybator.util.Util;

public class CreateJavaEntity extends Create {

    public static void main(String[] args) {
        String tableNameFull = "T_ATTACH_UPLOAD";
        CreateJavaEntity test = new CreateJavaEntity();
        List<JavaDao> javaDaoList = test.init(tableNameFull);
        test.parse(javaDaoList, tableNameFull);

    }

    public List<JavaDao> init(String tableNameFull) {

        List<JavaDao> javaDaoList = new ArrayList<JavaDao>();
        try {
            Map<String, String> javaTypeMap = getDataTypeConversionProperty().getMap();
            ;
            Set<String> importPath = new HashSet<String>();
            List<TableDesc> tableDescs = getTableInfo(tableNameFull);
            for (TableDesc tableDesc : tableDescs) {
                JavaDao javaDao = new JavaDao();
                String[] propertyName = Util.getPropertyName(tableDesc.getColumnName());
                javaDao.setComments(tableDesc.getRemarks());
                if (propertyName.length == 2) {
                    javaDao.setJavaType(propertyName[0]);
                    javaDao.setPropertyNameLow(propertyName[1]);
                    javaDao.setPropertyNameUp(propertyName[1].substring(0, 1).toUpperCase() + propertyName[1].substring(1));
                } else {
                    javaDao.setJavaType(Util.getJavaType(tableDesc, javaTypeMap));
                    javaDao.setPropertyNameLow(propertyName[0]);
                    javaDao.setPropertyNameUp(propertyName[0].substring(0, 1).toUpperCase() + propertyName[0].substring(1));
                }
                String jdbcType = Util.getEntityType(tableDesc, javaTypeMap);
                javaDao.setJdbcType(jdbcType);
                if (jdbcType.toLowerCase().equals("biginteger"))
                    importPath.add("java.math.BigInteger;");
                else if (jdbcType.toLowerCase().equals("bigdecimal"))
                    importPath.add("java.math.BigDecimal;");
                else if (jdbcType.toLowerCase().equals("date")) {
                    importPath.add("java.util.Date;");
                }
                javaDaoList.add(javaDao);
            }
            return javaDaoList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return javaDaoList;
    }

    public boolean parse(List<JavaDao> javaDaoList, String tableNameFull) {

        VelocityContext context = new VelocityContext();

        String tableName = tableNameFull.replaceAll("T_", "");
        String className = Util.getDOName(tableName);
        String filePath = Constants.createFilePath + className + "Entity.java";
        String modelPackagePath =Constants.modelPackagePath ;

        context.put("package", modelPackagePath);
        context.put("className", className + "Entity");
        context.put("propertyList", javaDaoList);
        List<String> pathList = new ArrayList<String>();

        context.put("pathList", pathList);

        Util.writeFile("entity_java.vm", context, filePath);
        return false;
    }

}
